﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Reflection;

namespace GU.WebERP.Core.AjaxFx
{
    /// <summary>
    /// Ajax Class 管理器。通过反射获取标记有 AjaxFxClassAttribute 的类。并对其进行调用。
    /// </summary>
    public static class AjaxFxClassManager
    {
        private static Dictionary<string, AjaxFxClassInfo> AjaxClasses = new Dictionary<string, AjaxFxClassInfo>();

        /// <summary>
        /// 初始化 Ajax Class 管理器，获取所有 Ajax 类
        /// </summary>
        /// <param name="assembly"></param>
        public static void Initialize(Assembly assembly)
        {
            lock (AjaxClasses)
            {
                if (AjaxClasses.Count <= 0)
                {
                    AjaxClasses = GetAjaxClasses(assembly);
                }
            }
        }

        /// <summary>
        /// 查找调用的方法
        /// </summary>
        /// <param name="context">HttpContext 数据信息</param>
        /// <returns></returns>
        public static AjaxFxMethodInfo FindMethod(HttpContext context)
        {
            string code = context.Request.QueryString["method"];
            if (string.IsNullOrEmpty(code))
            {
                return null;
            }

            foreach (AjaxFxClassInfo info in AjaxClasses.Values)
            {
                if (info.Methods.ContainsKey(code.ToLower()))
                {
                    return info.Methods[code.ToLower()];
                }
            }

            return null;
        }

        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="context">HttpContext 数据信息</param>
        /// <param name="info">方法信息</param>
        /// <returns></returns>
        public static AjaxFxRspJson CallMethod(HttpContext context, AjaxFxMethodInfo info)
        {
            object obj = System.Activator.CreateInstance(info.Method.DeclaringType);
            return (AjaxFxRspJson)info.Method.Invoke(obj, new object[] { context });
        }

        /// <summary>
        /// 获取所有标记有 AjaxFxClassAttribute 的类
        /// </summary>
        /// <param name="assembly">程序集</param>
        /// <returns></returns>
        private static Dictionary<string, AjaxFxClassInfo> GetAjaxClasses(Assembly assembly)
        {
            var dict = new Dictionary<string, AjaxFxClassInfo>();
            Type[] types = assembly.GetTypes();
            foreach (var type in types)
            {
                object[] attribs = type.GetCustomAttributes(typeof(AjaxFxClassAttribute), false);
                if (attribs != null && attribs.Length > 0)
                {
                    AjaxFxClassAttribute attrib = (AjaxFxClassAttribute)attribs[0];
                    if (dict.ContainsKey(attrib.Code))
                    {
                        throw new Exception("AjaxFxClass Code : " + attrib.Code + " 已被其它类使用请确认");
                    }
                    else
                    {
                        dict.Add(attrib.Code.ToLower(), new AjaxFxClassInfo(type, attrib.Code.ToLower(), attrib.Description));
                    }
                }
            }
            return dict;
        }
    }
}
